package com.tjetc.service.Impl;

import com.tjetc.common.JsonResult;
import com.tjetc.dao.ImageMapper;
import com.tjetc.entity.Image;
import com.tjetc.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageMapper imageMapper;

    /**
     * 添加轮播图到对应的商家，本函数先会将当前商家的所有轮播图都删掉，然后再添加
     * @param images 传入的String列表为图像的路径列表
     * @param merchantId 传入的merchantId为商家的id
     * @return 返回前端处理对象
     */
    @Transactional
    @Override
    public JsonResult add(List<String> images, Integer merchantId) {
        LocalDateTime time = LocalDateTime.now();
        Integer integer = imageMapper.removeByMerchantId(merchantId);
        for(String item : images) {
            Image image = new Image();
            image.setImagePath(item);
            image.setCreateTime(time);
            image.setMerchantId(merchantId);
            image.setUpdateTime(time);
            imageMapper.add(image);
        }
        return new JsonResult(0, "", true);
    }

    /**
     * 查询当前商家所有轮播图
     * @param merchantId 商家的id
     * @return 返回前端对象，包括轮播图的列表
     */
    @Override
    public JsonResult search(Integer merchantId) {
        return new JsonResult(0, "", imageMapper.searchByMerchantId(merchantId));
    }
}
